﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lib.JsonParser
{
    class JsonPrint
    {
        // print to console a json object format
        static public void printJson(Object json)
        {
            if (json is Dictionary<String, Object>)
                printJsonObject((Dictionary<String, Object>)json);
            else if (json is List<Object>)
                printJsonArray((List<Object>)json);
            else
                Console.WriteLine("Object not in a json object fromat");
        }

        static private void printJsonObject(Dictionary<String, Object> jsonObject)
        {
            Console.WriteLine("OBJECT START");
            foreach (var pair in jsonObject)
            {
                if (pair.Value != null)
                {
                    Console.WriteLine("{0}  :  {1}", pair.Key, pair.Value.ToString());
                }
                else
                {
                    Console.WriteLine("{0}  : null", pair.Key);
                }
            }
            Console.WriteLine("OBJECT END");
        }

        static private void printJsonArray(List<Object> list)
        {
            Console.WriteLine("ARRAY START");
            foreach (Object o in list)
            {
                if (o is Dictionary<String, Object>)
                {
                    printJsonObject((Dictionary<String, Object>)o);
                }
                else
                {
                    Console.WriteLine("unsopported object in a json array : {0} ", o.GetType());
                }
            }
            Console.WriteLine("ARRAY END");
        }
    }
}
